<?php
declare (strict_types=1);

namespace app\api\controller;


use app\model\DishesModel;
use app\model\IndentModel;
use app\model\StaffModel;
use app\model\UserModel;
use think\facade\Request;
use think\facade\Validate;

class Indent
{
    //获取订单列表
    public function list()
    {
        $params = Request::param();
        $params['indent_status'] = $params['indent_status'] ?? 0;
        $params['indent_id'] = $params['indent_id'] ?? 0;
        $validate = Validate::rule([
            'indent_status|订单状态' => 'require'
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }
        $model = IndentModel::where('indent_id', '>', 0);
        if($params['indent_status']!= 0){
            $model->where("indent_status","=",$params['indent_status']);
        };
        if($params['indent_id']!= 0){
            $model->where("indent_id","like",'%'.$params['indent_id'].'%');
        };
        $IndentList=$model->order('update_time desc')->select();
        foreach ($IndentList as &$value){
            //操作员
            $staffOne = StaffModel::find($value['staff_id']);
            $value['username'] = $staffOne['username'] ?? 'null';
            //用户
            $userOne=UserModel::where("open_id","=",$value['open_id'])->find();
            $value['nickname'] = $userOne['nickname'] ?? 'null';
            //支付状态
            if($value['indent_status'] == 1){
                $value['indent_status_name'] = '已支付';
            }else if($value['indent_status'] == 2){
                $value['indent_status_name'] = '待支付';
            }else if($value['indent_status'] == 3){
                $value['indent_status_name'] = '退款中';
            }else if($value['indent_status'] == 4){
                $value['indent_status_name'] = '已退款';
            }

            //获取该订单的所有菜品信息及菜品数量
            $dishesArr = explode(',',$value['dishes_id']);
            $dishes_amount = explode(',',$value['dishes_amount']);
            $dishesList = [];
            foreach ($dishesArr as $key=>$item){

                $dishesOne = DishesModel::find($item);
                $dishesOne['number'] = $dishes_amount[$key];
                array_push($dishesList,$dishesOne);
            }
            $value['dishesList'] = $dishesList;
        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'indentList' => $IndentList,
                'indent_status' => $params['indent_status']
            ],
        ];
        return json($data);
    }
    
     public function chart_sales_volume(){
//        $month=Request::param();
        $month=[1,2,3,4,5,6,7,8,9,10,11,12];
        //一个月时间戳
        $times=2592000;
        //2021年1月时间戳
        $ones=1609430400;
        $result=[];
//        $old=0;
        foreach($month as $item=>$value){
            $sales=IndentModel::where('add_time','between',[$ones,$ones+$times])->select();
            $result[]=$sales->count();
            $ones=$ones+$times;
//            $oness=$ones+$times;
            // echo $value.'月'.$ones."<br/>";
        }
//        var_dump($result);

       $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? $result: ''
            ]
        ];
        return json($data);
    }
    
     //总金额接口
    public function chart_money()
    {
        $month = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
        //一个月时间戳
        $times = 2592000;
        //2021年1月时间戳
        $ones = 1609430400;
        $result = [];
        $sum=0;
        $moneys=[];
        foreach ($month as $item => $value) {
            $sales = IndentModel::where('add_time', 'between', [$ones, $ones + $times])
                ->where('indent_status','=',1)
                ->field('pay_price')
                ->select();
            $result[] = $sales->toArray();
            foreach ( $result as $item=>$values){
                foreach ($values as $i=>$s){
                    $sum+=$s['pay_price'];
                }
                $sums=$sum;
                $sum=0;
            }
            $ones = $ones + $times;
            $moneys[]=$sums;
        }
        $data = [
            'status' => $moneys ? 0 : 10001,
            'message' => $moneys ? '' : '修改数据库失败',
            'data' => [
                'result' => $moneys ? $moneys : ''
            ]
        ];
        return json($data);
    }
    
    //模糊查询
    public function Like()
    {
        $params=Request::param();
        //订单号的模糊查询
        $validate = Validate::rule([
            'indent_id|订单号' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['indent_id' => $params['indent_id']])) {
            echo $validate->getError();
            exit();
        }
        $indent = IndentModel::where('indent_id','LIKE',$params['indent_id'])->select();
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'indentList' => $indent,
            ],
        ];
        return json($data);
    }
    //状态查询
    public function statusList()
    {
        $params=Request::param('indent_status');
        //订单号的状态的精准查询
        $validate = Validate::rule([
            'indent_status|订单号' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['indent_status' => $params])) {
            echo $validate->getError();
            exit();
        }
        $indent = IndentModel::where('indent_status','=',$params)->select();
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'indentList' => $indent,
            ],
        ];
        return json($data);
    }
    public function add()
    {
        //order
        $params = Request::param();
        $params['indent_status'] =  $params['indent_status'] ?? 1;
        $validate = Validate::rule([
            'indent_status|订单状态' => 'require',
            'pay_price|支付金额' => 'require',
            'dishes_id|所有菜品Id' => 'require',
            'dishes_amount|所有菜品数量' => 'require',
            'table_num|桌号' => 'require',
            'open_id|用户id'=>'require',
            'coupon_id|优惠卷id'=>'require'
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

//        if (!$params['staff_id']){
//            $params['staff_id']=null;
//        }
//        $params['dishes_status']=1;
        $params['add_time']=$params['update_time']=time();
        $result=IndentModel::create($params);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    //订单删除
    public function delete()
    {
        $IndentId = Request::param("indent_id");
        $validate = Validate::rule([
            'indent_id|订单号' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['indent_id' => $IndentId])) {
            echo $validate->getError();
            exit();
        }
        $Indent=DishesModel::find($IndentId);
        $result = DishesModel::destroy($Indent);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    
    public function charts()
    {
        $chartList = [];
        $indentCount = IndentModel::where("indent_status","=",1)->count();
        $chartList['indentCount'] = $indentCount;

        $data = [
            'status' => $indentCount ? 0 : 10001,
            'message' => $indentCount ? '' : '修改数据库失败',
            'data' => [
                'chartList' => $indentCount ? $chartList : false
            ]
        ];
        return json($data);
    }
    //订单修改
    public function update()
    {

        $params = Request::param();
        $validate = Validate::rule([
            'indent_id|订单id' => 'require|between:1,' . PHP_INT_MAX,
            'staff_id|员工id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

        $indentOne = IndentModel::find($params['indent_id']);
        
        $indentOne['staff_id'] = $params['staff_id'];
//
        $indentOne['update_time'] = time();
        $result = $indentOne->save();

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' => $result ? true : false
            ]
        ];
        return json($data);
    }
}
